package Java基础;

/**
 * @Author: biubiu0002
 * @Date: 2021-03-29 23:09
 **/
public class VolatileTest {
    public static void main(String[] args) {
        Volatile aVolatile = new Volatile();
        aVolatile.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if(aVolatile.isFlag()){
            System.out.println(Thread.currentThread().getName()+"读取到了volatile变量的变化");
        }
//        for(;;){
//            if(aVolatile.isFlag()){
//                System.out.println(Thread.currentThread().getName()+"读取到了volatile变量的变化");
//            }
//
//        }

    }

}

class Volatile extends Thread{
    private volatile boolean  flag = false;

    public boolean isFlag() {
        return flag;
    }

    public void run(){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        flag=true;
        System.out.println("flag="+flag);


    }

}
